﻿namespace Nello
{
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Web;

    public class FileUtil
    {
        private static void AddFileName(DirectoryInfo di, ArrayList list, params string[] filetypes)
        {
            ArrayList lst = new ArrayList();
            if (filetypes.Length > 0)
            {
                foreach (string s in filetypes)
                {
                    lst.Add(s);
                }
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if ((lst.Count == 0) || lst.Contains(fi.Extension))
                {
                    list.Add(fi.Name);
                }
            }
            foreach (DirectoryInfo d in di.GetDirectories())
            {
                AddFileName(d, list, filetypes);
            }
        }

        public static void AppendText(string path, string text)
        {
            StreamWriter sw = null;
            if (!File.Exists(path))
            {
                sw = File.CreateText(path);
            }
            else
            {
                sw = File.AppendText(path);
            }
            sw.WriteLine(text);
            sw.Close();
        }

        public static void DownloadFile(string fullPath)
        {
            string fileName = Path.GetFileName(fullPath);
            HttpResponse response = HttpContext.Current.Response;
            response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ";charset=GB2312");
            response.ContentType = "application/octet-stream";
            FileStream fs = File.OpenRead(fullPath);
            long fLen = fs.Length;
            int size = 0x19000;
            byte[] readData = new byte[size];
            if (size > fLen)
            {
                size = Convert.ToInt32(fLen);
            }
            long fPos = 0L;
            while (true)
            {
                if ((fPos + size) >= fLen)
                {
                    size = Convert.ToInt32((long) (fLen - fPos));
                    readData = new byte[size];
                    fs.Read(readData, 0, size);
                    response.BinaryWrite(readData);
                    fs.Close();
                    response.End();
                    return;
                }
                fs.Read(readData, 0, size);
                response.BinaryWrite(readData);
                fPos += size;
            }
        }

        public static ArrayList GetFileNames(string startpath, params string[] filetypes)
        {
            ArrayList list = new ArrayList();
            DirectoryInfo di = new DirectoryInfo(startpath);
            AddFileName(di, list, filetypes);
            return list;
        }

        public static string ReadAllText(string path)
        {
            if (!File.Exists(path))
            {
                return "";
            }
            return File.ReadAllText(path);
        }

        public int UploadFile(string folder, out ArrayList lstFileName, out string fileDescription)
        {
            string Msg = "";
            lstFileName = new ArrayList();
            string FilePath = HttpContext.Current.Request.MapPath(folder);
            int iCount = 0;
            HttpFileCollection files = HttpContext.Current.Request.Files;
            StringBuilder sbMsg = new StringBuilder();
            sbMsg.Append("上传的文件分别是：<hr color=red>");
            for (int i = 0; i < files.Count; i++)
            {
                try
                {
                    HttpPostedFile postedFile = files[i];
                    string fileName = Path.GetFileName(postedFile.FileName);
                    if (fileName != "")
                    {
                        string fileExtension = Path.GetExtension(fileName);
                        sbMsg.Append("上传的文件类型：" + postedFile.ContentType.ToString() + "<br>");
                        sbMsg.Append("客户端文件地址：" + postedFile.FileName + "<br>");
                        sbMsg.Append("上传文件的文件名：" + fileName + "<br>");
                        sbMsg.Append("上传文件的扩展名：" + fileExtension + "<br><hr>");
                        postedFile.SaveAs(FilePath + "/" + fileName);
                        iCount++;
                        lstFileName.Add(fileName);
                    }
                }
                catch (Exception ee)
                {
                    Msg = ee.Message;
                }
            }
            fileDescription = sbMsg.ToString();
            return iCount;
        }
    }
}

